const Service = require("egg").Service;

class StudentService extends Service {
    async add({ student_name, student_password, student_code, grade_id }) {
        let sql =
            "insert into student (name, code, gid, password) values (?, ?, ?, ?)";
        let params = [student_name, student_code, grade_id, student_password];
        let res = await this.app.mysql.query(sql, params);
        return res;
    }

    async readList() {
        let sql = `
            select
                s.name as student_name,
                s.password as student_password,
                s.code as student_code,
                g.name as grade_name,
                r.sn as room_name
            from student as s
            join grade as g on s.gid = g.id
            join room as r on g.room_id = r.id
        `;
        let res = await this.app.mysql.query(sql);
        return res;
    }
}

module.exports = StudentService;
